package com.ysh.smartplatform.service.impl;

import com.ysh.smartplatform.cache.AreaCache;
import com.ysh.smartplatform.event.AreaChangeEvent;
import com.ysh.smartplatform.mapper.AreaMapper;
import com.ysh.smartplatform.pojo.SysArea;
import com.ysh.smartplatform.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 袁胜虎 on 2022/9/17 16:12
 */
@Service
@Transactional
public class AreaServiceImpl implements AreaService {
    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    private AreaCache areaCache;

    @Autowired
    public void setAreaCache(AreaCache areaCache) {
        this.areaCache = areaCache;
    }

    private AreaMapper areaMapper;

    @Autowired
    public void setAreaMapper(AreaMapper areaMapper) {
        this.areaMapper = areaMapper;
    }



    @Override
    public void interArea(SysArea area) {
        areaMapper.interArea(area);
        context.publishEvent(new AreaChangeEvent());
    }

    @Override
    public List<SysArea> findAllArea() {
        List<SysArea> areaList = areaCache.getAreaList();
        return areaList;
    }

    @Override
    public void updateArea(SysArea area) {
        areaMapper.updateArea(area);
        context.publishEvent(new AreaChangeEvent());
    }

    @Override
    public void deleteByName(String name) {
        areaMapper.deleteByName(name);
        context.publishEvent(new AreaChangeEvent());
    }

    @Override
    public Object findByName(String name) {
        areaCache.get(name);
        return null;
    }
}
